﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcShop.Models.Repositories
{
    public class OrderRepository : BaseRepositoryBase<Orders>, IOrderRepository<Orders>
    {
        public OrderRepository()
        {
            this.EntitiesSetName = "Orderses";
        }

        public decimal GetOrdersTotal(string userName)
        {
            var query = (from od in this.EntitySet
                         where od.UserName == userName && od.IsPayment == false
                         select od.TotalValue).ToList();

            return query != null ? query.Sum() : 0;
        }
    }

    public class OrdersInfoRepository : BaseRepositoryBase<OrdersInfo>, IOrdersInfoRepository<OrdersInfo>
    { 
        public OrdersInfoRepository()
        {
            this.EntitiesSetName="OrdersInfos";
        }
    }

    public class DeliverRepository : BaseRepositoryBase<Deliver>, IDeliverRepository<Deliver>
    {
        public DeliverRepository()
        {
            this.EntitiesSetName = "Delivers";
        }
    }

    public class AddressBookRepository:BaseRepositoryBase<AddressBook>, IAddressBookRepository<AddressBook>
    {
        public AddressBookRepository()
        {
            this.EntitiesSetName = "AddressBooks";
        }

        public bool Update(AddressBook addressBook)
        {
            var query = (from add in this.EntitySet
                         where add.AddressBookId == addressBook.AddressBookId && add.UserName == addressBook.UserName
                         select add).FirstOrDefault();

            bool has = (query != null);
            if (has)
            {
                query = addressBook;
                this.SaveChanges();
            }
            return has;
        }

        public AddressBook Find(int id, string userName)
        {
            var query = (from add in this.EntitySet
                         where add.AddressBookId == id && add.UserName == userName
                         select add).FirstOrDefault();
            return query;
        }
    }
}
